<?php
include '../../../include/cp_header.php';
if ( file_exists("../language/".$xoopsConfig['language']."/admin.php") ) {
	include "../language/".$xoopsConfig['language']."/admin.php";
} else {
	include "../language/english/admin.php";
}
if ( file_exists("../language/".$xoopsConfig['language']."/main.php") ) {
	include "../language/".$xoopsConfig['language']."/main.php";
} else {
	include "../language/english/main.php";
}
function is_bad_environment() {
   $error = "";
   $cachedir = XOOPS_ROOT_PATH."/modules/xoopsgallery/cache";
   $albumdir = XOOPS_ROOT_PATH."/modules/xoopsgallery/cache/albums";
   $tmpdir = XOOPS_ROOT_PATH."/modules/xoopsgallery/cache/tmp";
   if (! (file_exists($cachedir) || mkdir($cachedir, 0705)) ) {
      $error .= _XG_ERROR00." {$cachedir} "._XG_ERROR01."<br>"._XG_ERROR03;   
   } elseif (! (is_writeable($cachedir) && is_readable($cachedir))) {
      $error .= _XG_ERROR00." {$cachedir} "._XG_ERROR02."<br>"._XG_ERROR04;      
   }
   if (! (file_exists($albumdir) || mkdir($albumdir, 0705)) ) {
      $error .= _XG_ERROR00." {$albumdir}"._XG_ERROR01."<br>"._XG_ERROR03;   
   } elseif (! (is_writeable($albumdir) && is_readable($albumdir))) {
      $error .= _XG_ERROR00." {$albumdir} "._XG_ERROR02."<br>"._XG_ERROR04;      
   }
   if (! (file_exists($tmpdir) || mkdir($tmpdir, 0705)) ) {
      $error .= _XG_ERROR00." {$tmpdir} "._XG_ERROR01."<br>"._XG_ERROR03;   
   } elseif (! (is_writeable($tmpdir) && is_readable($tmpdir))) {
      $error .= _XG_ERROR00." {$tmpdir} "._XG_ERROR02."<br>"._XG_ERROR04;      
   }
   if ($error != "") $error .= "<br>";
   return $error;
}
function ShowMain() {
    global $xoopsDB, $xoopsModule;
	xoops_cp_header();
	echo "<h4>"._XG_NETPBM72."</h4>";
	echo "<div style='font-weight: bold; font-size:120%;color:red;'>".is_bad_environment()."</div>";
	echo " - <a href='".XOOPS_URL."/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod="
	.$xoopsModule->getVar('mid')."'>"._XG_NETPBM73."</a>";
	echo "<br />"._XG_NETPBM74;
	echo "<br /><br />";
	echo " - <a href='index.php?op=check_netpbm'>"._XG_NETPBM01."</a>";
	echo "<br />"._XG_NETPBM75;
	echo "<br /><br />";
	echo " - <a href='index.php?op=check_imagemagick'>"._XG_NETPBM27."</a>";
	echo "<br />"._XG_NETPBM76;
	echo "<br /><br />";
	echo " - <a href='index.php?op=check_orphan'>"._XG_NETPBM60."</a>";
	echo "<br />"._XG_NETPBM61._XG_NETPBM62._XG_NETPBM63._XG_NETPBM64._XG_NETPBM65._XG_NETPBM66;
	echo "<br /><br />";
	echo " - <a href='index.php?op=check_missing'>"._XG_NETPBM67."</a>";
	echo "<br />"._XG_NETPBM68._XG_NETPBM69._XG_NETPBM70._XG_NETPBM66;
	echo "<br /><br />";
	echo " - <a href='index.php?op=check_perms'>"._XG_NETPBM77."</a>";
	echo "<br />"._XG_NETPBM78;
	echo "<br /><br />";
	xoops_cp_footer();
}
function admin_CheckNetPBM() {
	global $xoopsUser, $xoopsModule, $xoopsModuleConfig;
	xoops_cp_header();
	require('../check_netpbm.php');
	xoops_cp_footer();
}
function admin_CheckImageMagick() {
	global $xoopsUser, $xoopsModule, $xoopsModuleConfig;
	xoops_cp_header();
	require('../check_imagemagick.php');
	xoops_cp_footer();
}
function admin_OrphanCheck() {
	global $xoopsDB;
	xoops_cp_header();
	echo '<h4>'._XG_NETPBM60.'</h4>';
	// Look for problems in comment records
	$sql = 'select count(*) from '.$xoopsDB->prefix('xoopscomments').' where com_exparams like \'%xoops_imageid=%\'';
	$result = $xoopsDB->query($sql);
	list($total_bad_comment_exparams) = $xoopsDB->fetchRow($result);
	if ($total_bad_comment_exparams > 0) {
		printf(_XG_NETPBM80, $total_bad_comment_exparams);
	} else {
		echo _XG_NETPBM81;
	}
	// Orphaned records - Get total record count
	$sql = 'select count(*) from '.$xoopsDB->prefix('xoopsgallery_image');
	$result = $xoopsDB->query($sql);
	list($total_record_count) = $xoopsDB->fetchRow($result);
	printf("<br /><br /><p>"._XG_NETPBM82."</p>", $total_record_count);
	unset($result);
	// Orphaned records - Get list
	$sql = 'select * FROM '.$xoopsDB->prefix('xoopsgallery_image');
	$result = $xoopsDB->query($sql);
	$bad_id_list = "";
    if ( $total_record_count > 0 ) {
		while(list($image_id, $image_name, $image_ext, $image_type, $image_albumdir, $image_basedir, $image_created, $image_comments) = $xoopsDB->fetchRow($result)) {
			$fname = $image_basedir.'/'.$image_albumdir.'/'.$image_name.'.'.$image_ext;
			if (!file_exists($fname)) {
				$bad_id_list .= ','.$image_id;
				echo _XG_NETPBM83." $fname ($image_id)<br />";
			}
		}
	}
	if (strlen($bad_id_list) == 0) {
		echo _XG_NETPBM84;
	} else {
		$bad_id_list = substr($bad_id_list, 1);
	}
	if (strlen($bad_id_list) || $total_bad_comment_exparams > 0) {
		// Confirm remove bad records
		echo "<br /><br /><form action=\"index.php\" method=\"POST\">\n";
		echo "<input type=\"hidden\" name=\"op\" value=\"remove_orphan\"></input>";
		echo "<input type=\"hidden\" name=\"bad_id_list\" value=\"$bad_id_list\"></input>";
		echo "<br />"._XG_NETPBM85."<br />  <input type=\"submit\" value=\""._XG_NETPBM86."\"></form>\n";
		echo "</form>";
	}
	xoops_cp_footer();
}
function admin_RemoveOrphan() {
	global $xoopsDB;
	xoops_cp_header();
	echo '<h4>'._XG_NETPBM86.'</h4>';
	// Fix for bad comment exparams (refetch count because I'm too lazy to look up affected rows)
	$sql = 'select count(*) from '.$xoopsDB->prefix('xoopscomments').' where com_exparams like \'%xoops_imageid=%\'';
	$result = $xoopsDB->query($sql);
	list($total_bad_comment_exparams) = $xoopsDB->fetchRow($result);
	if ($total_bad_comment_exparams > 0) {
		printf("<br />"._XG_NETPBM88."<br />", $total_bad_comment_exparams);
		$sql = 'update '.$xoopsDB->prefix('xoopscomments').' set com_exparams = replace(com_exparams, \'xoops_imageid\', \'unused_data\') where com_exparams like \'%xoops_imageid=%\'';
		$result = $xoopsDB->query($sql);
	}
	// remove any orphans found
	$bad_id_arry = explode(',', $_POST['bad_id_list']);
	$count = count($bad_id_arry);
	if ($count && $bad_id_arry[0] != "") {
		$xoopsgalleryimage_handler =& xoops_getmodulehandler('image');
		for ($i = 0; $i < $count; $i++) {
			echo '<br />'._XG_NETPBM89.' '.$bad_id_arry[$i];
			$xoopsgallery_image =& $xoopsgalleryimage_handler->get($bad_id_arry[$i]);
			if (is_object($xoopsgallery_image)) {
				$xoopsgalleryimage_handler->delete($xoopsgallery_image);
				xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $bad_id_arry[$i]);
			}
		}
	}
	echo '<br /><br />'._XG_NETPBM90;
	xoops_cp_footer();
}
function admin_CheckTable() {
	global $xoopsUser, $xoopsModule, $xoopsModuleConfig;
	xoops_cp_header();
	require('../check_table.php');
	xoops_cp_footer();
}
function admin_CheckFolderPerms() {
	global $xoopsUser, $xoopsModule;
	$check = is_bad_environment();
	xoops_cp_header();
	echo "<h4>"._XG_NETPBM77."</h4>";
	if ($check == "") {
		echo "<div style='font-weight: bold; font-size:120%;color:green;'>". _XG_NETPBM79 ."</div>";
	} else {
		echo "<div style='font-weight: bold; font-size:120%;color:red;'> $check </div>";
	}
	xoops_cp_footer();
}
if(!isset($_POST['op'])) {
	$op = isset($_GET['op']) ? $_GET['op'] : 'main';
} else {
	$op = $_POST['op'];
}
switch ($op) {
case 'check_netpbm':
	admin_CheckNetPBM();
	break;
case 'check_imagemagick':
	admin_CheckImageMagick();
	break;
case 'check_orphan':
	admin_OrphanCheck();
	break;
case 'remove_orphan':
	admin_RemoveOrphan();
	break;
case 'check_missing':
	admin_CheckTable();
	break;
case 'check_perms':
	admin_CheckFolderPerms();
	break;
case 'main':
default:
	ShowMain();
	break;
}
?>